package com.cj.user.service.demo;

import java.util.*;

/**
 * @Author cheng jun
 * @Description:
 * @Date 2021/5/15 21:55
 */
public class Demo6 {

    //计算字符串最后一个单词的长度
    public static int calculatedLength(String str) {
        //长度
        int strLength;
        String[] strArray = str.split(" ");
        strLength = strArray[strArray.length - 1].length();
        return strLength;
    }

    /**
     * 写出一个程序，接受一个由字母、数字和空格组成的字符串，和一个字母，然后输出输入字符串中该字母的出现次数。不区分大小写，字符串
     * 长度小于500。
     * @return
     */
    public static int numberOccurrences(String str, char c) {
        char[] cArray = str.toLowerCase().toCharArray();
        //记录次数
        int count = 0;

        //char字符转小写
        if (c > 'A' && c < 'Z') {
            c += 32;
        }
        for (int i = 0; i < cArray.length; i++) {
            if (c == cArray[i]) {
                count++;
            }
        }
        return count;
    }

    /**
     * 连续输入字符串，请按长度为8拆分每个字符串后输出到新的字符串数组；
     * 长度不是8整数倍的字符串请在后面补数字0，空字符串不处理。
     * @param strArray
     * @return
     */
    public static Object[] tringSegmentation(String[] strArray) {
        //存储字符串的数组
        StringBuffer[] strArray2 = new StringBuffer[]{};
        //存储入参字符串的集合
        List<String> list= new ArrayList<>(Arrays.asList(strArray));
        //存储字符串的集合
        List<StringBuffer> list2= new ArrayList<>(Arrays.asList(strArray2));
        //遍历入参
        list.forEach(item -> {
            int length = item.length();
            if (length <= 8) {
                StringBuffer stringBuffer = new StringBuffer();
                stringBuffer.append(item);
                for (int i = 0; i < 8 - length; i++) {
                    stringBuffer.append(0);
                }
                list2.add(stringBuffer);
            } else if (length % 8 == 0) {
                for (int i = 0; i < length / 8; i++) {
                    StringBuffer stringBuffer = new StringBuffer();
                    stringBuffer.append(item, 8 * i, 8 * (i + 1));
                    list2.add(stringBuffer);
                }
            } else {
                for (int i = 0; i < length / 8; i++) {
                    StringBuffer stringBuffer = new StringBuffer();
                    stringBuffer.append(item, 8 * i, 8 * (i + 1));
                    list2.add(stringBuffer);
                }

                //最后不满8个
                StringBuffer str = new StringBuffer();
                str.append(item.substring((length - (length % 8))));
                for (int i = 0; i < 8 - (length % 8); i++) {
                    str.append(0);
                }
                list2.add(str);
            }
        });
        return list2.toArray();
    }

    /**
     * 输入一个十六进制的数值字符串。注意：一个用例会同时有多组输入数据，
     * 请参考帖子https://www.nowcoder.com/discuss/276处理多组输入的问题。
     * @param content
     * @return
     */
    public static int baseConversion(String content){
        int number=0;
        String [] HighLetter = {"A","B","C","D","E","F"};
        Map<String,Integer> map = new HashMap<>();
        for(int i = 0;i <= 9;i++){
            map.put(i+"",i);
        }
        for(int j= 10;j<HighLetter.length+10;j++){
            map.put(HighLetter[j-10],j);
        }
        String[]str = new String[content.length()];
        for(int i = 0; i < str.length; i++){
            str[i] = content.substring(i,i+1);
        }
        for(int i = 0; i < str.length; i++){
            number += map.get(str[i])*Math.pow(16,str.length-1-i);
        }
        return number;
    }

    /**
     * 输入一个整数，将这个整数以字符串的形式逆序输出
     * 程序不考虑负数的情况，若数字含有0，则逆序形式也含有0，如输入为100，则输出为001
     * @return
     */
    public static StringBuffer numberInversion(Integer number) {
        String[] strArray = number.toString().split("");
        //存储遍历后的数
        StringBuffer stringBuffer = new StringBuffer();
        for (int i = strArray.length - 1; i >= 0; i--) {
            stringBuffer.append(strArray[i]);
        }
        return stringBuffer;
    }

    public static void main(String[] args) {
        //System.out.println(calculatedLength("hello nowcoder kobe"));
        //System.out.println(numberOccurrences("avAaAFDCFA", 'a'));
        /*Object[] strArray = tringSegmentation(new String[]{"abc", "123456789", "eqeqqqqqqqqqqqqqqqqq", "2"});
        for (int i = 0; i < strArray.length; i++) {
            System.out.println(strArray[i]);
        }*/
        /*String content = "0fA";
        if(!content.matches("[0-9a-fA-F]*")){
            System.out.println("输入不匹配");
            System.exit(-1);
        }
        //将全部的小写转化为大写
        content = content.toUpperCase();
        System.out.println(baseConversion(content));*/
        System.out.println(numberInversion(1516000));
    }
}
